<template>
  <base-modal :show.sync="visible" :title="dialogTitle" width="600px" class="progress-manage-dialog">
    <el-table v-loading="listLoading" :data="dataList" max-height="500" header-row-class-name="common-list-table-header__v2" border>
      <template slot="empty">
        <BaseListForNoData v-show="!listLoading" :notice-msg="$t('common.base.tip.noData')"></BaseListForNoData>
      </template>

      <el-table-column v-for="(column, index) in columns" :key="`${column.displayName}_${index}`" :prop="column.fieldName" :label="column.displayName" show-overflow-tooltip>
        <template slot-scope="scope">
          <template v-if="column.fieldName === 'taskName'"> 
            <div>
              {{ scope.row.taskName }}
              <el-tag class="mar-r-5 mar-b-12" effect="plain" v-if="scope.row.isPreTask"> {{ $t('projectManage.setting.taskSetting.presetTag') }}</el-tag>
            </div>
          </template>
          <template v-else-if="column.fieldName === 'taskType'"> {{ taskTypeMap[scope.row.taskType] }}/{{ scope.row.taskFormName }} </template>
          <template v-else-if="column.fieldName === 'taskWeight'">
            <!-- 草稿/已取消状态 权重不可以编辑 -->
            <input type="number" v-model="scope.row.taskWeight" :style="{ width: '80px' }" :disabled="scope.row.taskStatus === 'DRAFT' || scope.row.taskStatus === 'CANCEL'" @change="changeTaskWeight(scope.row)" />
          </template>
          <template v-else>
            {{ $formatFormField(column, scope.row) }}
          </template>
        </template>
      </el-table-column>
    </el-table>

    <div slot="footer" class="dialog-footer">
      <el-button @click="resetForm">{{ $t('common.base.cancel') }}</el-button>
      <el-button type="primary" :loading="showLoading" @click="submit">{{ $t('common.base.submit') }}</el-button>
    </div>
  </base-modal>
</template>
<script>
import { reactive, toRefs } from 'vue';
import { processFixTable, taskTypeMap } from '@src/modules/projectManage/productTypeUtils/fixFields.js';
import { projectProcessSimpleList, projectUpdateWight } from '@src/api/ProjectManage.ts';

import { toast } from '@src/util/platform';
import MathUtil from '@src/util/math';
import Platform from '@src/util/platform.ts';
import i18n from '@src/locales';

export default {
  name: 'progress-manage-dialog',
  props: {
    projectId: {
      type: String | Number,
      default: '',
    },
  },
  emits: ['progressSubmit'],
  setup(props, { emit }) {
    const state = reactive({
      visible: false,
      dialogTitle: i18n.t('common.projectManage.detail.progressManagement'),
      showLoading: false,
      listLoading: false,
      dataList: [],
      columns: processFixTable,
    });

    // 获取
    const fetchTableList = async () => {
      state.listLoading = true;
      try {
        let ret = await projectProcessSimpleList({
          projectId: props.projectId,
          pageSize: 500,
          filterDraftAndCancelStatus: true
        });

        const { success, data, message } = ret;
        if (!success) return toast(message, 'error');

        state.dataList = data?.list || [];
      } catch (err) {
        console.log('error => fetchFields', err);
      } finally {
        state.listLoading = false;
      }
    };

    const openDialog = () => {
      fetchTableList();
      state.visible = true;
    };

    const resetForm = () => {
      state.visible = false;
    };

    const submit = async () => {
      let infoList = state.dataList.map(item => {
        return {
          taskId: item.id,
          weight: Number(item.taskWeight),
        };
      });
      state.showLoading = true;
      try {
        let ret = await projectUpdateWight({
          projectId: props.projectId,
          infoList: infoList || [],
        });

        const { success, data, message } = ret;
        if (!success) return toast(message, 'error');

        toast(i18n.t('common.base.tip.updateSuccess'), 'success');
        resetForm();

        emit('progressSubmit');
      } catch (err) {
        console.log('error => submit projectUpdateWight', err);
      } finally {
        state.showLoading = false;
      }
    };

    // 修改数量
    const changeTaskWeight = item => {
      let value = Number(item.taskWeight);
      let count = MathUtil.decimalNumber(value);

      if (value <= 0 || value > 100) {
        Platform.alert(i18n.t('common.projectManage.detail.errorTip'));
        return;
      }

      if (count > 2) {
        Platform.alert(i18n.t('common.form.tip.materialReturn.tips9'));
      }
    };

    return {
      taskTypeMap,
      ...toRefs(state),
      openDialog,
      resetForm,
      submit,
      changeTaskWeight,
    };
  },
};
</script>
<style lang="scss" scoped>
.progress-manage-dialog {
  ::v-deep .base-modal-body {
    padding: 20px;
  }
}
</style>